<%@ page contentType="text/html; charset=UTF-8" language="java" pageEncoding="UTF-8"%>
<%@include file="/common/taglibs.jsp"%>
<%@include file="/common/javascript.jsp" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>角色</title>
    <link rel="stylesheet" href="${ctx}/static/ztree/css/demo.css" type="text/css" />
	<link rel="stylesheet" href="${ctx}/static/ztree/css/zTreeStyle/zTreeStyle.css" type="text/css" />
    <script type="text/javascript">
    var zTree;
	var zNodes;
    var mainform;
    var viewType = "${viewType}";
	if (viewType != "detail")
	{
		var setting = {
				check: {enable: true},
				view: {dblClickExpand: false},
				data: {simpleData: {enable: true}}
			};
	}
	else
	{
		var setting = {
				check: {enable: false},
				view: {dblClickExpand: false},
				data: {simpleData: {enable: true}}
			};
	}
	$.ajax({
		   type: "POST",
		   url: "${ctx}/menu/rightTree?roleId=${command.roleId}",
		   cache: true,
		   async:false,
		   dataType:'json',
		   success: function(result){
			   if (typeof(result)== "string") {
				   result = eval("(" + result + ")");
			   }
			   zNodes=result;
		   }
		});
    $(function (){
      
    	$("#tabcontainer").ligerTab();
    	
    	zTree=$.fn.zTree.init($("#treeDemo"), setting,zNodes);
    	
        mainform=$("#mainform");
        
        mainform.ligerForm();
        
        if(viewType=="modify"){
        	 mainform.attr("action","${ctx}/role/update"); 
        	 $("#roleId").attr("readonly", "readonly").removeAttr("validate");
        }else{
        	mainform.attr("action","${ctx}/role/add");
        }
        
        if(viewType=="detail"){
        	$("input,select,textarea",mainform).attr("disabled", "disabled");
        }else{
            $.metadata.setType("attr", "validate");
            LG.validate(mainform);
        }
        
        LG.setFormDefaultBtn(cancel,viewType=="detail" ? null : save);

    });
    
    function setMenu(){
    	var nodes=zTree.getCheckedNodes();
    	var node;
    	var mIds="";
    	var rIds="";
    	for(var i=0;i<nodes.length;i++){
    		node=nodes[i];
    		if(node.type=="menu"){
    			mIds=mIds+node.id+",";
    		}else{
    			rIds=rIds+node.id+",";
    		}
    	}
    	$("#menuIds").val(mIds);
    	$("#resourceIds").val(rIds);
    }
    
    function save(){
    	setMenu();
        LG.submitForm(mainform, function (result) {
        	if(typeof(result)=="string"){
        		result=eval("("+result+")");
        	}
            if (!result.success) {
                LG.showError('错误:' + data.message);
            }
            else { 
                LG.showSuccess('保存成功', function () { 
                   LG.callParent("roleList");
                });
            }
        });
    }
    function cancel(){
        var win = window.parent || window;
        if(win.dialog){
        	win.dialog.close();
        }
    }
 </script>
</head>

<body>
<div id="tabcontainer" style="margin:0px;">
     <div title="基本信息">
    <form name="mainform" method="post"  id="mainform">
        <input type="hidden" name="viewType" value="${viewType}"/>
    	<input type="hidden" name="menuIds" id="menuIds"/>
    	<input type="hidden" name="resourceIds" id="resourceIds"/>
        <table cellpadding="0" cellspacing="0" class="l-table-edit" style="width: 100%;">
            <tr>
                <td align="left" class="l-table-edit-td" style="width:150px;padding-left: 10px;" >代码:</td>
                <td align="left" class="l-table-edit-td" style="width:100px" >
                	<input name="roleId" type="text" id="roleId" value="${command.roleId }" ltype="text" validate="{required:true,minlength:3,maxlength:10,notnull:true}" nullText="不能为空!"/>
                </td>
                <td align="left"></td>
            </tr>
            <tr>
                <td align="left" class="l-table-edit-td" style="width:150px;padding-left: 10px;"  valign="top">名称:</td>
                <td align="left" class="l-table-edit-td" style="width:100px">
                	<input name="roleName" type="text" id="roleName" value="${command.roleName }" ltype="text" validate="{required:true,minlength:3,maxlength:10,notnull:true}" nullText="不能为空!"/>
                </td>
                <td align="left"></td>
            </tr>   
             <tr>
                <td align="left" class="l-table-edit-td" style="width:150px;padding-left: 10px;" valign="top">启用:</td>
                <td align="left" class="l-table-edit-td" style="width:100px">
                	<c:choose>
                		<c:when test="${command.isEnable eq 'F' }">
                			<input id="isEnable" type="checkbox" name="isEnable" value="F" onchange="changeTnf(this)"/> 
                		</c:when>
                		<c:otherwise>
                			<input id="isEnable" type="checkbox" name="isEnable" value="T" checked="checked"  onchange="changeTnf(this)"/> 
                		</c:otherwise>
                	</c:choose>
                </td>
                <td align="left"></td>
            </tr>
            <tr>
                <td align="left" class="l-table-edit-td" style="width:150px;padding-left: 10px;" >描述:</td>
                <td align="left" class="l-table-edit-td"> 
                <input name="roleDescription" type="text" id="roleDescription" value="${command.roleDescription }" ltype="text" validate="{required:true,minlength:3,maxlength:40,notnull:true}" nullText="不能为空!"/>
                </td> <td align="left"></td>
            </tr>
            <tr>
                <td align="left" class="l-table-edit-td" style="width:150px;padding-left: 10px;" >备注:</td>
                <td align="left" class="l-table-edit-td"> 
                <textarea cols="100" rows="3" class="l-textarea" id="remark" name="remark" style="width:400px">${command.remark}</textarea>
                </td> <td align="left"></td>
           </tr>
        </table>
    </form>
    </div>
    <div title="分配权限" tabid="tab_permissions">
		<ul id="treeDemo" class="ztree" style="height:300px;width:100%;margin-top:0px;"></ul>
     </div>
</div>
</body>
</html>
